package com.baizhi.controller;

import com.baizhi.pojos.Shop;
import com.baizhi.pojos.User;
import com.baizhi.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 根据不同的账号权限登录查询不同的内容
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/shopList")
    public String shopList(Model model, HttpSession session){

        User user = (User)session.getAttribute("user");

        Map<String,Object> list = shopService.findList(user);

        model.addAttribute("shop",list.get("shop"));

        model.addAttribute("userAndRole",list.get("userAndRole"));

        return "shopList";
    }

    /**
     * 访问添加页面
     * @return
     */
    @GetMapping("/toadd")
    public String toadd(){

        return "add";
    }

    /**
     * 实现添加
     * @param shop
     * @return
     */
    @PostMapping("/addShop")
    public String addShop(Shop shop){

        shopService.save(shop);

        return "redirect:/shop/shopList";
    }

    /**
     * 修改回显
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/findById")
    public String findById(Integer id, Model model){

        Shop shop = shopService.getById(id);

        model.addAttribute("s",shop);

        return "update";
    }

    /**
     * 修改功能
     * @param shop
     * @return
     */
    @PostMapping("/updateShop")
    public String update(Shop shop){

        shopService.updateById(shop);

        return "redirect:/shop/shopList";
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @GetMapping("/delById")
    public String delById(Integer id){

        shopService.removeById(id);

        return "redirect:/shop/shopList";
    }

    /**
     * 提交审核
     * @param id
     * @return
     */
    @GetMapping("/getOn")
    public String getOn(Integer id){

        Shop byId = shopService.getById(id);

        byId.setStatus("1");

        shopService.updateById(byId);

        return "redirect:/shop/shopList";
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @GetMapping("/pass")
    public String pass(Integer id){

        Shop byId = shopService.getById(id);

        byId.setStatus("2");

        shopService.updateById(byId);

        return "redirect:/shop/shopList";
    }

    /**
     * 上架
     * @param id
     * @return
     */
    @GetMapping("/up")
    public String up(Integer id){

        Shop byId = shopService.getById(id);

        byId.setStatus("4");

        shopService.updateById(byId);

        return "redirect:/shop/shopList";
    }

    /**
     * 下架
     * @param id
     * @return
     */
    @GetMapping("/down")
    public String down(Integer id){

        Shop byId = shopService.getById(id);

        byId.setStatus("5");

        shopService.updateById(byId);

        return "redirect:/shop/shopList";
    }
}
